package com.tangula.voicenote

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.tangula.android.base.TglBasicActivity
import com.tangula.android.utils.AndroidSettingsUtils
import com.tangula.android.utils.PermissionUtils
import com.tangula.android.utils.ToastUtils
import com.tangula.voicenote.service.AddOverlayService
import com.tangula.voicenote.service.DatabaseService
import kotlinx.android.synthetic.main.activity_main.*
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ImageView
import android.view.animation.AlphaAnimation
import com.tangula.voicenote.db.NoteDbHelper
import com.tangula.voicenote.entity.NoticeEntity


class MainActivity : TglBasicActivity() {

    @SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val action = supportActionBar
        if(action!=null){
          action.hide()
        }

        val scaleAnimation1 =
            ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        scaleAnimation1.setDuration(500);
        val ima1 = findViewById<ImageView>(R.id.vw_drop_down)
        ima1.animation = scaleAnimation1
        scaleAnimation1.startNow()

        val scaleAnimation2 =
            ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        scaleAnimation2.startOffset = 500
        scaleAnimation2.setDuration(500);
        val ima2 = findViewById<ImageView>(R.id.vw_dock)
        ima2.animation = scaleAnimation2
        scaleAnimation2.startNow()

        val scaleAnimation3 =
            ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        scaleAnimation3.startOffset = 1000
        scaleAnimation3.setDuration(500);
        val ima3 = findViewById<ImageView>(R.id.vw_resize)
        ima3.animation = scaleAnimation3
        scaleAnimation3.startNow()

        val alphaAnimation = AlphaAnimation(0.8f, 0f)
        alphaAnimation.duration = 500
        alphaAnimation.startOffset = 1500
        alphaAnimation.repeatCount = Animation.INFINITE
        val ima4 = findViewById<ImageView>(R.id.vw_done)
        ima4.animation = alphaAnimation





        AndroidSettingsUtils.canOverlayOrNot(this, {
            PermissionUtils.whenFirstGrantPremissions(this, listOf(
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ),  {
                val list = NoteDbHelper().listAllNotes()
                if(list.isEmpty()){
                    Log.v("console", "notes is empty")
                    AddOverlayService.addOverlayService()
                }else {
                    Log.v("console", "notes size is ${list.size}")
                    for (note in list) {
                        Log.v("console", "note: ${note.id} , ${(note as NoticeEntity).message}")
                        AddOverlayService.addOverlayServiceById(note.id)
                    }
                }
            }){
                noOverlayPermsNotice()
            }
           }){
            noAudioPermsNotice()
        }

        vw_done.setOnClickListener {
            DatabaseService.exec { db->
                if(db.listNotices().isEmpty()){
                    AddOverlayService.addOverlayService()
                }else{
                    val list = NoteDbHelper().listAllNotes()
                    if(list.isEmpty()){
                        Log.v("console", "notes is empty")
                        AddOverlayService.addOverlayService()
                    }else {
                        Log.v("console", "notes size is ${list.size}")
                        for (note in list) {
                            Log.v("console", "note: ${note.id} , ${(note as NoticeEntity).message}")
                            AddOverlayService.addOverlayServiceById(note.id)
                        }
                    }
                }
            }
            finish()
        }

    }

    fun noOverlayPermsNotice(){
        Log.w("console", "not grant overlay permissions")
        ToastUtils.showToastResLong(R.string.toast_no_overlay_perms)
    }

    fun noAudioPermsNotice(){
        Log.w("console", "not grant audio and storage permissions")
        ToastUtils.showToastResLong(R.string.toast_no_audio_perms)
    }

    override fun onCreateView(name: String?, context: Context?, attrs: AttributeSet?): View? {
        return super.onCreateView(name, context, attrs)
    }
}
